import pickle

class UserCache:
    def __init__(self, cache_file):
        self.cache_file = cache_file
        try:
            f = open(self.cache_file, "r+b")
            self.dict = pickle.load(f)
        except:
            self.dict = {}
    def readCache(self, id):
        try:
            return self.dict[id]
        except KeyError:
            return None
    def writeCache(self, theUser):
        self.dict[theUser.id] = theUser
        self.dict[theUser.screen_name] = theUser
    def saveCache(self):
        f = open(self.cache_file, "w+b")
        pickle.dump(self.dict, f, pickle.HIGHEST_PROTOCOL)